package application.ratt.user_interface;

import java.util.ArrayList;
import java.util.HashMap;


import application.ratt.R;
import application.ratt.utils.Route;
import application.ratt.utils.RoutesXMLParser;
import application.ratt.utils.TimeInStation;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class ResultsActivity extends Activity implements OnItemClickListener{
	
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.results_main);

    final ListView listview = (ListView) findViewById(R.id.sampleListView);
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    int i = 1;
    for (Route r : RoutesXMLParser.getRoutes()) {
    	HashMap<String, String> map = new HashMap<String, String>();
    	 map.put("routeName", i + ". " + r.getRouteName());
         map.put("routeTime", r.getRouteTime());
         list.add(map);
         i++;
    }
    
    SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.results_simplerow, new String[] {"routeName", "routeTime"}, new int[] {R.id.text1, R.id.text3});

    listview.setAdapter(sa);
    listview.setOnItemClickListener(this);
  }

  @Override
	public void onItemClick(AdapterView<?> adapter, final View v, int position, long id) {
		// TODO Auto-generated method stub
	    String item = "";
	    final ArrayList<String> listItem = this.getRouteDetails(position);
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		
		for(String s : listItem){
			item = item  + s + "\n";
		}
		item = item.substring(0, item.length()-1);
		
		alert.setTitle("Route details");
		alert.setMessage(item);
		
		alert.setPositiveButton("OK",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					//	v.setBackgroundColor(Color.WHITE);
						dialog.cancel();
					}
				});
		
		alert.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			    //	v.setBackgroundColor(Color.WHITE);
			    }
				return false;
			}
		});
		
	//	v.setBackgroundColor(Color.GRAY);
		alert.show();
  	}
  
  	private ArrayList<String> getRouteDetails(int routeNumber){
  		Route route = null;
  		ArrayList<String> infoStations = new ArrayList<String>();
  		boolean addedToList;
  		int timeValue = 0;
  		
  		route = RoutesXMLParser.getRoutes().get(routeNumber);
  		for (TimeInStation s : route.getStations()){
  			String stationName = s.getName();
  			String lineName = s.getLineName();
  			
  			timeValue += s.getTime();
  			
  			addedToList = false;
  			
  			if (-1 == timeValue){
  				//is start element
  				if (stationName.length()>25){
  	  				stationName = stationName.substring(0, 25) + "\u2026";
  	  			}
  				infoStations.add(stationName);
  				addedToList = true;
  			}
  			
			if (!addedToList) {
				if (stationName.length() > 15) {
					stationName = stationName.substring(0, 13) + "\u2026";
				}
				if (lineName.length() > 5) {
					lineName = lineName.substring(0, 5);
				} else {
					infoStations.add(stationName + " - " + lineName + " - " + timeValue + "min");
				}
			}
  		}
	  return infoStations;
  	}
} 